#include "lrcpage.h"
#include "ui_lrcpage.h"
#include <QFile>
#include <QDebug>

LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);

    // 初始化动画
    lrcAnimation = new QPropertyAnimation(this, "geometry", this);
    lrcAnimation->setDuration(250);
    lrcAnimation->setStartValue(QRect(10, 16, width(), height()));
    lrcAnimation->setEndValue(QRect(10, 16 + height(), width(), height()));

    connect(ui->hideBtn, &QPushButton::clicked, this, [=]{
            // this->hide();
            lrcAnimation->start();
        });
    // 动画结束，窗口隐藏
    connect(lrcAnimation, &QPropertyAnimation::finished, this, [=]()
    {
       this->hide();
    });

    ui->hideBtn->setIcon(QIcon(":/images/xiala.png"));
    showLrcWord(-1);
}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::parseLrc(const QString &lrcPath)
{
    QFile lrcFile(lrcPath);
    if (!lrcFile.open(QFile::ReadOnly))
    {
        qDebug() <<"打开文件失败" << lrcPath;
        return false;
    }

    // 将上一首歌词清空
    lrcLines.clear();
    while (!lrcFile.atEnd())
    {
        QString lrcWord = lrcFile.readLine(1024);
        // [00:17.94]那些失眠的啊 你们还好吗
        // [0:58.600.00]你像只来去的蝴蝶
        int left = lrcWord.indexOf('[');
        int right = lrcWord.indexOf(']');
        //解析时间
        int start = 0;
        int end = 0;
        qint64 lineTime = 0;
        QString time = lrcWord.mid(left, right - left + 1);

        // 解析分钟
        start = end + 1;
        end = time.indexOf(':');
        lineTime += lrcWord.mid(start, end - start).toUInt()*60*1000;
        // 解析秒
        start = end + 1;
        end = time.indexOf('.', start);
        lineTime += lrcWord.mid(start, end - start).toUInt()*1000;
        // 解析毫秒
        start = end + 1;
        end = time.indexOf('.', start);
        lineTime += lrcWord.mid(start, end - start).toUInt();
        // 解析歌词
        QString word = lrcWord.mid(right + 1).trimmed();
        // 保存
        lrcLines.push_back(LyricLine(lineTime, word));
    }

    // 验证
    for (auto word : lrcLines)
    {
        qDebug() << word.time << " " << word.text;
    }

    return true;
}

int LrcPage::getLineLrcWordIndex(qint64 pos)
{
    if (lrcLines.empty())
    {
        return -1;
    }

    if (lrcLines[0].time > pos)
    {
        return 0;
    }

    // 时间比较，获取下标
    for (int i = 1; i < lrcLines.size(); i++)
    {
        if (pos > lrcLines[i - 1].time && pos <= lrcLines[i].time)
        {
            return i - 1;
        }
    }

    // 没有找到就是最后部分
    return lrcLines.size() - 1;
}

QString LrcPage::getLineLrcWord(qint64 index)
{
    if (index < 0 || index >= lrcLines.size())
    {
        return "";
    }

    return lrcLines[index].text;
}

void LrcPage::showLrcWord(int time)
{
    // 获取歌词索引
    int index = getLineLrcWordIndex(time);
    if (index == -1)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineCenter->setText("当前歌曲无歌词");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }
    else
    {
        ui->line1->setText(getLineLrcWord(index - 3));
        ui->line2->setText(getLineLrcWord(index - 2));
        ui->line3->setText(getLineLrcWord(index - 1));
        ui->lineCenter->setText(getLineLrcWord(index));
        ui->line4->setText(getLineLrcWord(index + 1));
        ui->line5->setText(getLineLrcWord(index + 2));
        ui->line6->setText(getLineLrcWord(index + 3));
    }
}

void LrcPage::setTitle(const QString &name, const QString &singer)
{
    ui->musicName->setText(name);
    ui->musicSinger->setText(singer);
}
